package com.learn.queue.safety.unblocking;

import com.learn.constant.MathConstant;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.*;

/**
 * 测试ConcurrentLinkedQueue
 */
public class ConcurrentLinkedQueueTest {

    private static ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();
    private static CountDownLatch latch1 = new CountDownLatch(MathConstant.INT_FIVE);
    static ExecutorService pool = Executors.newFixedThreadPool(MathConstant.INT_FIVE);

    public static void getNumber(){
        for(int i=MathConstant.INT_ZERO;i<MathConstant.INT_FIVE;i++){
            pool.submit(() -> {
                while (!queue.isEmpty()) {
                    System.out.println(Thread.currentThread().getName() + "===" + queue.poll());
                }
                latch1.countDown();
            });
        }
    }

    public static void getNumber2(){
        for(int i = MathConstant.INT_ZERO;i < MathConstant.INT_FIVE;i++){
            pool.submit(() -> {
                while (!queue.isEmpty()) {
                    System.out.println(Thread.currentThread().getName() + "===" + queue.peek());
                }
                latch1.countDown();
            });
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException, ExecutionException, InterruptedException {
        for(int i = MathConstant.INT_ZERO;i <= MathConstant.INT_200;i++){
            queue.offer(i);
        }
        getNumber();
        latch1.await();
        System.out.println(queue.size());
        pool.shutdown();
    }
}
